<%@ page language="java" contentType="text/html; charset=utf8"%>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
	<link rel="stylesheet" href="${pageContext.request.contextPath}/statics/css/page.css">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/statics/easyui/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/statics/easyui/themes/icon.css">
	<script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/form.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/statics/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/statics/easyui/jquery.easyui.min.js"></script>
	<link rel="stylesheet" href="${pageContext.request.contextPath}/statics/css/form.css">
	<script>
		$(function(){
			$("#id").val('${role.id}');
			$("#permission").tree({
				url:"${pageContext.request.contextPath}/admin/permission/tree",
				textField:'name',
				valueField:'id',
				checkbox:true,
				onLoadSuccess:function(){
					var permission = '${role.permissionsStr}';
					if(permission.length == 0) return;
					var ps = permission.split(",");
					for(var p in ps){
						var node = $('#permission').tree('find', parseInt(ps[p]));  
						if(node && node.target){
							$('#permission').tree('check',node.target);
						}
					}
			    }
			})
			$("#btnCancel").click(function(){
				close();
			});
			$("#btnOk").click(function(){
				$.ajax({
					url:'${pageContext.request.contextPath}/admin/role/assignpermission',
					type:"post",
					dataType:'html',      
					data:{
						id:$("#id").val(),
						permissions:getPermission()
					},
					success:function(data){
						close();
					}
				})
			})
		});
		
		function getPermission(){
			var permission = "";
			var nodes = $('#permission').tree('getChecked');
			for(var i=0; i<nodes.length; i++){
				permission += nodes[i].id + ","
			}
			if(permission.length > 0){
				permission = permission.substr(0, permission.length-1);
			}
			return permission;
		}
	</script>
</head>
<body>
	<form id="form" class="lv-form-container">
		<input type="hidden" id="id"/>
		<div class="lv-control-row">
			<div id="permission" class="lv-single-control" style="height:200px;"></div>
		</div>
		<div class="lv-control-row">
			<div class="lv-btn" id="btnCancel">取消</div>
			<div class="lv-btn" id="btnOk">确定</div>
		</div>
	</form>
</body>
</html>